package org.jeecg.modules.app.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.app.entity.AppClient;
import org.jeecg.modules.app.entity.AppUpgrade;
import org.jeecg.modules.app.enums.AppPkgTypeEnums;
import org.jeecg.modules.app.enums.AppPlatformTypeEnums;
import org.jeecg.modules.app.mapper.AppClientMapper;
import org.jeecg.modules.app.mapper.AppUpgradeMapper;
import org.jeecg.modules.app.service.IAppUpgradeService;
import org.jeecg.modules.app.vo.UpgradeVO;
import org.springframework.stereotype.Service;

import java.util.Arrays;

/**
 * @Description: 更新管理
 * @Author: sean
 * @Date:   2024-09-28
 * @Version: V1.0
 */
@Service
@RequiredArgsConstructor
public class AppUpgradeServiceImpl extends ServiceImpl<AppUpgradeMapper, AppUpgrade> implements IAppUpgradeService {

    private final AppClientMapper appClientMapper;

    @Override
    public IPage<AppUpgrade> selectJoinPage(Page page, Wrapper wrapper) {
        IPage<AppUpgrade> pageList = baseMapper.selectJoinPage(page, wrapper);
        if(oConvertUtils.isNotEmpty(pageList) && oConvertUtils.isNotEmpty(pageList.getRecords())){
//            pageList.getRecords().stream().forEach(item->{
//            });
        }
        return pageList;
    }

    @Override
    public UpgradeVO androidUpgradeNew(String appid, Integer versionCode) {
        return this.newUpgradeInfo(appid, versionCode, AppPlatformTypeEnums.ANDROID);
    }

    @Override
    public UpgradeVO iosUpgradeNew(String appid, Integer versionCode) {
        return this.newUpgradeInfo(appid, versionCode, AppPlatformTypeEnums.IOS);
    }

    @Override
    public UpgradeVO newUpgradeInfo(String appid, Integer versionCode, AppPlatformTypeEnums appPlatformType) {
        AppClient appClient = appClientMapper.getOneNoTenant(new LambdaQueryWrapper<AppClient>()
                .eq(AppClient::getAppId, appid).eq(AppClient::getPlatformType, appPlatformType.getValue()));

        AppUpgrade appUpgrade = baseMapper.getMaxVersionRecord(appClient.getId(), null);
        UpgradeVO upgradeVO = new UpgradeVO();
        if(ObjectUtil.isNotEmpty(appUpgrade)
                && StrUtil.isNotBlank(appUpgrade.getUrl())
        ){
            int result = appUpgrade.getVersionCode().compareTo(versionCode);
            if(result > 0){
                //需要更新
                if(AppPkgTypeEnums.WGT.getValue().equals(appUpgrade.getPkgType())) { //配置版本的包是增量包
                    //这种情况查看看中间是否有 整包，有些更新必须整包更新，所以中间有整包，必须先更新整包
                    //找到最后APK包记录,可能是安装包也可以时应用市场地址
                    AppUpgrade pkgUpgrade = baseMapper.getMaxVersionRecord(appClient.getId(), Arrays.asList(AppPkgTypeEnums.PKG.getValue(), AppPkgTypeEnums.MARKET.getValue()));
                    int pkgResult = pkgUpgrade.getVersionCode().compareTo(versionCode);
                    if(ObjectUtil.isNotEmpty(pkgUpgrade)) {
                        if (pkgResult > 0
                                && StrUtil.isNotBlank(pkgUpgrade.getUrl())) {
                            upgradeVO.setIsUpdate(Boolean.TRUE);
                            upgradeVO.setUrl(pkgUpgrade.getUrl());
                            upgradeVO.setNote(pkgUpgrade.getUpgradeContent());
                            upgradeVO.setPkgType(pkgUpgrade.getPkgType());
                            upgradeVO.setIsForce("1".equals(pkgUpgrade.getForceUpdate()) ? Boolean.TRUE : Boolean.FALSE);
                        } else {
                            upgradeVO.setIsUpdate(Boolean.TRUE);
                            upgradeVO.setUrl(appUpgrade.getUrl());
                            upgradeVO.setNote(appUpgrade.getUpgradeContent());
                            upgradeVO.setPkgType(appUpgrade.getPkgType());
                            upgradeVO.setIsForce("1".equals(appUpgrade.getForceUpdate()) ? Boolean.TRUE : Boolean.FALSE);
                        }
                    }else{
                        upgradeVO.setIsUpdate(Boolean.TRUE);
                        upgradeVO.setUrl(appUpgrade.getUrl());
                        upgradeVO.setNote(appUpgrade.getUpgradeContent());
                        upgradeVO.setPkgType(appUpgrade.getPkgType());
                        upgradeVO.setIsForce("1".equals(appUpgrade.getForceUpdate()) ? Boolean.TRUE : Boolean.FALSE);
                    }
                }else{
                    //其他情况直接返回结果前端
                    upgradeVO.setIsUpdate(Boolean.TRUE);
                    upgradeVO.setUrl(appUpgrade.getUrl());
                    upgradeVO.setNote(appUpgrade.getUpgradeContent());
                    upgradeVO.setPkgType(appUpgrade.getPkgType());
                    upgradeVO.setIsForce( "1".equals(appUpgrade.getForceUpdate())? Boolean.TRUE : Boolean.FALSE );
                }
            }else{
                upgradeVO.setIsUpdate(Boolean.FALSE);
            }
        }else{
            upgradeVO.setIsUpdate(Boolean.FALSE);
        }
        return upgradeVO;
    }
}
